--
-- Author: ping.wang
-- Date: 2018-09-19 17:35:53
--
-- CrystalController
--

local CrystalView = import("..views.CrystalView")
local CrystalInlayView = import("..views.CrystalInlayView")
local NewguildTips = require("app.newGuild.views.NewguildTips")
local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("CrystalModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end

--强化水晶界面
function ClassRef:openStrengthView(data)
    if cc.isDead(self._view) then
        self._view = CrystalView.new(app.mvc.VIEWTYPE_BLUR,data)
        self._view:onCloseView(handler(self, self.closeStrengthView))
        self._view:onClearView(handler(self, self.clearStrengthView))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view, data)

    self._view:show()
end
function ClassRef:clearStrengthView()
    if self._view then
        self._view = nil
    end
end

function ClassRef:closeStrengthView()
    if self._view then
        self:removeChild(self._view)
        self._view = nil
    end
end

--镶嵌水晶界面
function ClassRef:openInlayView(data)
    if nil == self._inlayView then
        self._inlayView = CrystalInlayView.new(app.mvc.VIEWTYPE_BLUR,data)
        self._inlayView:onCloseView(handler(self, self.closeInlayView))
        self._inlayView:onClearView(handler(self, self.clearInlayView))
        self._inlayView:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._inlayView, data)

    self._inlayView:show()
end
function ClassRef:clearInlayView()
    if self._inlayView then
        self._inlayView = nil
    end
end

function ClassRef:closeInlayView()
    if self._inlayView then
        self:removeChild(self._inlayView)
        self._inlayView = nil
    end
end

-------------------------------------------------------


-------------------------------------------------------
function ClassRef:onOperate(op, data)
    --print("CrystalController:onOperate op = " .. op)
    if op == "strengthCrystal" then
        self:startLevelUpCrystal(data.isOnEquip,data.itemId,data.equipUid,data.itemUid)
    elseif op == "showCrystalOnEquipTips" then
        --显示镶嵌在装备上的提示
        self:_showTips(data.render,data.crystalId,data.equipUid,nil,true)
    elseif op == "showCrystalTips" then
        --显示右侧水晶提示
        self:_showTips(data.render,data.crystalId,data.equipUid,data.crystalUid,false)
    elseif op == "showJingTongTips" then
        self:showJingTongTips(data[1],data[2],data[3])
    end
end

function ClassRef:startLevelUpCrystal(isOnEquip,crystalId,equipUid,crystalUid)
    self._model:reqLevelUpCrystal(isOnEquip,crystalId,equipUid,crystalUid)
end

--成功升级水晶
function ClassRef:successLevelUpCrystal(isOnEquip,crystalId,crystalUid,equipUid)
    if not cc.isDead( self._view ) then
        --刷新强化界面
        self._view:setData(isOnEquip,crystalId,crystalUid,equipUid)
    end

    if not cc.isDead( self._inlayView ) then
        --刷新镶嵌
        self._inlayView:refresh()
    end
end

--成功镶嵌水晶
function ClassRef:successInsertCrystal(equipUid)
    if not cc.isDead( self._inlayView ) then
        --刷新镶嵌
        self._inlayView:refresh()
    end
end

--成功移除镶嵌水晶
function ClassRef:successRemoveCrystal(equipUid)
    if not cc.isDead( self._inlayView ) then
        --刷新镶嵌
        self._inlayView:refresh()
    end
end
--
function ClassRef:_showTips(render, crystalId, equipUid, crystalUid, isOnEquip)
    local params = {
        tipsType = Enums.TipsType.item,
        itemId = crystalId,
        uid = crystalUid,
        equipUid = equipUid,
        isOnEquip = isOnEquip
    }

    if equipUid and crystalId then
        --是否已经镶嵌了足够的水晶
        params.haveEnoughTypeRock = self:isEquipHaveEnoughTypeRock(equipUid, crystalId)
    end

    -- 如果是水晶 则显示一个强化按钮
    params.crystalUpgradeCallback = function()
        UIHandler:handleMessage( { id = Constant.FUNC_TYPE_TIPS_CRYSTAL_LVUP, mode = "open", data = {itemUid = crystalUid,
            itemId = crystalId, equipUid = equipUid, isOnEquip = isOnEquip} } )
        self._inlayView:clearTips()
    end

    if isOnEquip then
        --如果是在装备上的水晶
        --显示一个卸下
        params.crystalTakeOutCallback = function(eqUid, cryId)
            --直接镶嵌
            self._model:reqRemoveCrystal(eqUid,cryId)
            self._inlayView:clearTips()
        end
    else
        --如果不是在装备上的水晶
        --显示一个镶嵌
        params.crystalInlayIndirectCallback = function(eqUid, cryId, cryUid)
            --直接镶嵌,判断装备上是否已经有3种相同的水晶了 如果有 则提示镶嵌数量已经达到上限
            if self:isEquipHaveEnoughTypeRock(eqUid, cryId) then
                display.pushToast(L("rockTips_4"))
            else
                self._model:reqInsertCrystal(eqUid,cryUid)
            end

            self._inlayView:clearTips()
        end
    end

    self._inlayView:showTips(render, nil, params)
end

--同一类型的水晶最多只能镶嵌3个
function ClassRef:isEquipHaveEnoughTypeRock(eqUid, crystalId)
    local rockType = tonumber(GD:queryRockById(crystalId).type)
    local num = 0
    local equip = UD:getEquip(eqUid)
    if equip then
        local allCrystals = equip:getAllCrystalSlots()
        for k,v in ipairs(allCrystals) do
            if tonumber(GD:queryRockById(v).type) == rockType then
                num = num + 1
            end
        end
    end

    if num >= 3 then
        return true
    end
    return false
end

function ClassRef:showJingTongTips( titleStr, contentStr, pos )
    if cc.isDead( self._guildTips ) then
        self._guildTips = NewguildTips.create()

        self._inlayView:addChild( self._guildTips )
        -- 点击空白区域关闭
        display.nodeRemoveForNotHit(self._guildTips, function()
            if self._guildTips then
                self._guildTips:removeFromParent(true)
                self._guildTips = nil
            end
        end)
    end
    self._guildTips:setNameStr( titleStr )
    self._guildTips:setContentStr( contentStr )
    self._guildTips:setVisible( true )

    local width = self._guildTips:getContentSize2()
    local posX = pos.x
    if posX + width > display.width then
        posX = display.width - width
    end
    self._guildTips:setPosition( cc.p( posX, pos.y ) )
end

return ClassRef
